/* libstdf -- library for reading writing and manipulating STDF files and data.
   Copyright (C) 2009 Tushar Jog
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA 
*/

using System;
using System.Collections.Generic;
using System.IO;

using stdf;

using U1 = System.Byte;
using U2 = System.UInt16;
using I1 = System.SByte;
using I2 = System.Int16;
using U4 = System.UInt32;
using I4 = System.Int32;
using R4 = System.Single;
using R8 = System.Double;
//using C1 = System.SByte;
using C1 = System.Char;
using B1 = System.Byte;
using N1 = System.Byte;

namespace stdf {

    public class Utility {
        public static ByteOrder getNativeByteOrder() {
            ByteOrder byteOrder = ByteOrder.NotDefined;

            uint nativeInt = 0x04030201;
            //byte *pByte = (byte *) &nativeInt;

            // Hope this works
            byte[] pByte = BitConverter.GetBytes(nativeInt);

            switch(pByte[0]) {
                case 0x04: byteOrder = ByteOrder.BigEndian; break;
                case 0x01: byteOrder = ByteOrder.LittleEndian; break;
                case 0x03: byteOrder = ByteOrder.PDPEndian; break;
                default:   byteOrder = ByteOrder.NotDefined; break;
            };

            return byteOrder;
        }

//        public static void translate( byte[] src, ref U2 dest, bool swap) {
//            if(swap)
//                Array.Reverse( src);
//
//            byte *b = (byte *)&dest;
//            b[0] = src[0];
//            b[1] = src[1];
//        }
        public static void translate( byte[] src, U2 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToUInt16(src, 0);
        }
        public static void translate( byte[] src, I2 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToInt16(src, 0);
        }
        public static void translate( byte[] src, U4 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToUInt32(src, 0);
        }
        public static void translate( byte[] src, I4 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToInt32(src, 0);
        }
        public static void translate( byte[] src, R4 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToSingle(src, 0);
        }
        public static void translate( byte[] src, R8 dest, bool swap) {
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToDouble(src, 0);
        }



        public static U2 translateToU2( byte[] src, bool swap) {
            U2 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToUInt16(src, 0);
            return dest;
        }
        public static I2 translateToI2( byte[] src, bool swap) {
            I2 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToInt16(src, 0);
            return dest;
        }
        public static U4 translateToU4( byte[] src, bool swap) {
            U4 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToUInt32(src, 0);
            return dest;
        }
        public static I4 translateToI4( byte[] src, bool swap) {
            I4 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToInt32(src, 0);
            return dest;
        }
        public static R4 translateToR4( byte[] src, bool swap) {
            R4 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToSingle(src, 0);
            return dest;
        }
        public static R8 translateToR8( byte[] src, bool swap) {
            R8 dest = 0;
            if(swap)
                Array.Reverse( src);
            dest = BitConverter.ToDouble(src, 0);
            return dest;
        }
    }

}

